package fr.esgi.pages;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import fr.esgi.donnees.Database;


/**
 * Servlet implementation class ValidationUpload
 */
public class ValidationUpload extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ValidationUpload() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		int groupe = 0;
		HttpSession session = request.getSession();
		String uid=String.valueOf(session.getAttribute("uid"));
		System.out.println(uid);
		String projet=null;
		String livrable=null;
		Database c=new Database();
		PrintWriter out = response.getWriter();
		boolean isMultipart = ServletFileUpload.isMultipartContent(request);
		if (!isMultipart) {
		} else {
			FileItemFactory factory = new DiskFileItemFactory();
			ServletFileUpload upload = new ServletFileUpload(factory);
			List items = null;
			try {
				items = upload.parseRequest(request);
			} catch (FileUploadException e) {
				e.printStackTrace();
			}
			Iterator itr = items.iterator();
			while (itr.hasNext()) {
				FileItem item = (FileItem) itr.next();
				if (item.isFormField()) {
					String name = item.getFieldName();
					String value = new String(item.getString().getBytes("ISO-8859-1"), "UTF-8");
					if(name.equals("projet")) {
						projet=value; 
						System.out.println(projet);
					}
					else if(name.equals("livrable")){
						livrable=value; 
						System.out.println(livrable);
					}
				} 
			}
			try {
				groupe=c.recuperationGroupe(uid,projet);
			} catch (ClassNotFoundException e1) {
				// TODO Auto-><b>Your filegenerated catch block
				e1.printStackTrace();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			if(groupe!=0){
				Iterator itr2 = items.iterator();
				while (itr2.hasNext()) {
					FileItem item = (FileItem) itr2.next();
					if (!item.isFormField()) {
						try {
							String itemName = item.getName();
							File savedFile = new File("ges/"+projet+"/"+livrable+"/"+String.valueOf(groupe)+"/"+itemName);
							item.write(savedFile);  
							out.println("<tr><td has been saved at the loaction:</b></td></tr><tr><td><b>"+"/Users/antoine/Documents/ges/"+projet+"/"+livrable+"/"+"uploadedFiles"+"\\"+itemName+"</td></tr>");
							Date maintenant = new Date(); 
							SimpleDateFormat formatDateJour = new SimpleDateFormat("dd/MM/yyyy"); 
							String date = formatDateJour.format(maintenant); 
							c.insertOrUpdateRendu(projet, livrable, String.valueOf(groupe), date);		
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
			}
		}		
	}
}